package com.study.modules.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.modules.sys.mapper.SysMenuMapper;
import com.study.modules.sys.mapper.SysRoleMenuMapper;
import com.study.modules.sys.model.entity.SysMenu;
import com.study.modules.sys.model.entity.SysRoleMenu;
import com.study.modules.sys.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author Mr.Du
 * @Date 2021/10/18 15:07
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;
    @Autowired
    private SysMenuMapper menuMapper;

    @Override
    public Set<String> selectMenuByRoleId(Long roleId) {
        Set<Long> MenuIdList = roleMenuMapper.selectMenuIdList(roleId);
        Set<String> permList = new HashSet<>();
        for (Long menuId : MenuIdList) {
            String perms = menuMapper.selectMenuPermsById(menuId);
            if (perms != null) {
                if (perms.contains(",")) {
                    String[] strings = StrUtil.splitToArray(perms, ',');
                    List<String> menuPerms = Arrays.asList(strings);
                    permList.addAll(menuPerms);
                } else {
                    permList.add(perms);
                }
            }
        }
        return permList;
    }

    @Override
    public List<SysMenu> selectMenuListByRoleId(Long roleId) {
        Set<Long> menuIdList = baseMapper.selectMenuIdList(roleId);
        List<SysMenu> menuList = new ArrayList<>();
        if (menuIdList != null && menuIdList.size() > 0) {
            for (Long menuId : menuIdList) {
                SysMenu sysMenu = menuMapper.selectMenuById(menuId);
                menuList.add(sysMenu);
            }
        }
        return menuList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRoleMenu(Long roleId, List<Long> menuIdList){
        deleteRoleMenuByRoleId(roleId);
        if (menuIdList!=null && menuIdList.size() >0) {
            for (Long menuId : menuIdList) {
                SysRoleMenu roleMenu = new SysRoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                roleMenuMapper.insert(roleMenu);
            }
        }
        return true;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRoleMenuByMenuId(Long menuId) {
        Set<Long> menuIdList = baseMapper.selectRoleIdList(menuId);
        if (menuIdList != null && menuIdList.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("menu_id", menuId);
            roleMenuMapper.deleteByMap(map);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRoleMenuByRoleId(Long roleId) {
        Set<Long> menuIdList = baseMapper.selectMenuIdList(roleId);
        if (menuIdList != null && menuIdList.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("role_id", roleId);
            roleMenuMapper.deleteByMap(map);
        }
        return true;
    }

}
